import numpy as np


# 激活函数
def sigmoid(x):
    return 1 / (1 + np.exp(-x))


class Neuron:
    """单个神经元"""
    def __init__(self, weights, bias):
        self.weights = weights
        self.bias = bias

    def feedforward(self, inputs):
        # 通过激活函数进行前馈
        total = np.dot(self.weights, inputs) + self.bias
        return sigmoid(total)


class Feednn:
    """
    前馈神经网络:
        - 输入层 2 inputs
        - 隐藏层 2 neurons
        - 输出层 1 neurons
    """
    def __init__(self):
        weights = np.array([0, 1])
        bias = 0

        self.h1 = Neuron(weights, bias)  # 隐藏层neuron
        self.h2 = Neuron(weights, bias)  # 隐藏层neuron
        self.o1 = Neuron(weights, bias)  # 输出层neuron

    def feedforward(self, x):
        # 输入层前馈
        out_h1 = self.h1.feedforward(x)
        out_h2 = self.h2.feedforward(x)

        # 隐藏层前馈
        out_o1 = self.o1.feedforward(np.array([out_h1, out_h2]))

        return out_o1


if __name__ == '__main__':
    # 单个神经元
    weights = np.array([0, 1])  # w1 = 0, w2 = 1
    bias = 4  # b = 4
    n = Neuron(weights, bias)

    x = np.array([2, 3])  # x1 = 2, x2 = 3
    print(n.feedforward(x))  # 0.9990889488055994

    # feedforward神经网络
    network = Feednn()
    x = np.array([2, 3])
    print(network.feedforward(x))  # 0.7216325609518421
